home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / hoard.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-31  |  3.9 KB  |  156 lines

  1.  
  2.  
  3. #ifndef _HOARD_H_
  4. #define _HOARD_H_
  5.  
  6. #include <silib/splat.hpp>
  7. #if !defined(PLAT_WINDOWS32)
  8.   #define DLLFUNC
  9. #else
  10.   #include <windows.h>
  11.   #define DLLFUNC    WINAPI
  12. #endif
  13.  
  14. #if defined (SINGLE_THREADED)
  15. #define hmalloc malloc
  16. #define hcalloc calloc
  17. #define hfree   free
  18. #define hrealloc realloc
  19. #define hallocator allocator
  20.  
  21. #else
  22.  
  23. //
  24. //DEBUG_NEW screws us up as it stops us from using the placement new operator
  25. #ifdef new
  26. #undef new
  27. #endif
  28.  
  29. #include <new>
  30. #include <cstdlib>
  31. #ifdef __sun
  32. #include <limits.h>
  33. #endif // __sun
  34.  
  35. // SILIB now uses Hoard in a DLL - for more info, see "hoard.cpp"
  36. // To facilitate this, hmalloc, hfree & hrealloc are now function pointers that point to initializing functions _hmalloc, _hfree & _hrealloc
  37. // Hence, when hmalloc is first called, _hmalloc tries to load the Hoard DLL, get the function address and set hmalloc to that function
  38. // If it fails to get the function address from the DLL, then use the default implementation (i.e. malloc)  
  39. typedef void * (DLLFUNC * pMallocFunc) (size_t);
  40. typedef void (DLLFUNC * pFreeFunc) (void *);
  41. typedef void * (DLLFUNC * pReallocFunc) (void *, size_t);
  42.  
  43. extern pMallocFunc hmalloc;
  44. extern pFreeFunc hfree;
  45. extern pReallocFunc hrealloc;
  46.  
  47. #if !defined(__SUNPRO_CC) && !defined(PLAT_AIX)
  48. //
  49. // STL allocator equivalents using hoard
  50. //
  51. #pragma warning (disable:4786) // C4786 'identifier' : identifier was truncated to 'number' characters in the debug information
  52. template<class T>
  53. class hallocator 
  54. {
  55. public:
  56.     typedef size_t      size_type;
  57.     typedef ptrdiff_t   difference_type;
  58.     typedef T         * pointer;
  59.     typedef const T   * const_pointer;
  60.     typedef T         & reference;
  61.     typedef const T   & const_reference;
  62.     typedef T           value_type;
  63.  
  64. #if defined(__linux__) || (_MSC_VER >= 1300)
  65.  
  66.     hallocator()
  67.     {
  68.     }
  69.  
  70.     hallocator(const hallocator &)
  71.     {
  72.     }
  73.  
  74.  
  75.     template<class _Tp1> 
  76.         hallocator( const hallocator<_Tp1> &) {}
  77.  
  78.     template<class _Tp1> struct rebind {
  79.         typedef hallocator<_Tp1> other;
  80.     };
  81.  
  82. #endif // __linux__
  83.  
  84.  
  85.  
  86.     pointer address ( reference r ) const {
  87.         return (&r);
  88.     }
  89.     const_pointer address ( const_reference r ) const {
  90.         return (&r); 
  91.     }
  92. #ifdef __sun
  93.     void *allocate ( size_type _size, const void * hint = 0 ) {
  94. #else
  95.     pointer allocate ( size_type _size, const void * hint = 0 ) {
  96. #endif // __sun
  97.         //TODO: if someone is bored then figuring out how to incorporate the hint seems like a fun task =)
  98. #if defined (__SUNPRO_CC)
  99.         return hmalloc ( _size );
  100. #elif defined (_MSC_VER) || defined (__GNUC__)
  101.         return (pointer) hmalloc ( sizeof(value_type) * _size );
  102. #else
  103.         #error "check your compilers STL allocator implementation"
  104. #endif
  105.     }
  106.     void deallocate ( void * ptr, size_type ) {
  107.         hfree (ptr); 
  108.     }
  109.     void construct (pointer _ptr, const T& _t) {
  110.         new ((void*)_ptr) T(_t); 
  111.     }
  112.     void destroy ( pointer ptr ) {
  113.         (ptr)->~T();
  114.     }
  115.  
  116.     size_type max_size() const {
  117.         //2Gig worth sounds good
  118.         size_type twogigabytes = 2147483648;
  119.         return size_type (twogigabytes / sizeof(T));
  120.     }
  121. #ifdef __sun
  122.     size_type max_size (size_type size) const
  123.     {
  124.         return 1 > UINT_MAX/size ? size_type(1) : size_type(UINT_MAX/size);
  125.     }
  126. #endif // __sun
  127.     //seems this is a hack needed by MSVC
  128.     char  *_Charalloc ( size_type _size ) {
  129.         return (char*) hmalloc ( sizeof(value_type) * _size );
  130.     }
  131. };
  132. template<class T, class U> inline
  133. bool operator== ( const hallocator<T>&, const hallocator<U>& ) {
  134.     return true; 
  135. }
  136. template<class T, class U> inline
  137. bool operator!= ( const hallocator<T>&, const hallocator<U>& ) {
  138.     return false; 
  139. }
  140. template<> class hallocator<void> {
  141. public:
  142.     typedef void    T;
  143.     typedef T       *pointer;
  144.     typedef const T *const_pointer;
  145.     typedef T       value_type;
  146. };
  147.  
  148. #else
  149. #define hallocator allocator
  150. #endif
  151.     
  152. #endif
  153. #endif
  154.  
  155.  
  156.